//
//  FullScreenControlPanel.m
//  LabanBrowser
//
//  Created by hoangtx on 4/8/13.
//  Copyright (c) 2013 VNG. All rights reserved.
//

#import "FullScreenControlPanel.h"
#import "SGPageViewController.h"

@implementation FullScreenControlPanel{
    NSTimer* fadeTimer;
    BOOL isInTabState;
}

double maximumLastingTime=1.0f;
double fullScreenFadeDuration=0.5f;

@synthesize delegate;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (IBAction)backAction:(id)sender {
    if(isInTabState == YES){
        if(delegate && [delegate respondsToSelector:@selector(onFullScreenPressDoneButton)]){
            [delegate onFullScreenPressDoneButton];
        }
    }else{
        if(self.browser.canGoBack){
            [self.browser goBack];
        }else{
            [self.browser backToHomepage];
        }
    }
    
}

- (IBAction)forwardAction:(id)sender {
    if(isInTabState == YES){
        if(delegate && [delegate respondsToSelector:@selector(onFullScreenPressOpenRecentClosedTabsButton)]){
            [delegate onFullScreenPressOpenRecentClosedTabsButton];
        }
    }else{
        if(self.browser.canGoForward){
            [self.browser goForward];
        }else{
            if([self.browser.selectedViewController isKindOfClass:[HomePageViewController class]]){
                HomePageViewController* homeVC = (HomePageViewController*)self.browser.selectedViewController;
                [self.browser handleURLString:homeVC.lastURL title:homeVC.lastURL];
            }
        }
        
    }
    
}

- (IBAction)unFullScreenAction:(id)sender {
    if(isInTabState == YES){
//        if(delegate && [delegate respondsToSelector:@selector(onFullScreenPressAddNewTabButton)]){
//            [delegate onFullScreenPressAddNewTabButton];
//        }
        [self.browser addTab];
    }else{
        [self.browser performSelector:@selector(goUnfullscreen)];
    }
    
}

-(void)updateForwardButtonBasedOnUrlStack:(BOOL)_stackIsEmpty{
    if(isInTabState){
        if(_stackIsEmpty){
            self.forwardBT.alpha = 0;
            self.forwardIMV.alpha = 0;
        }else{
            self.forwardBT.alpha = 1;
            self.forwardIMV.alpha = 1;
        }
    }
    
}
-(void)swapState{
    if(isInTabState == YES){
        isInTabState = NO;
        self.multiTabBT.alpha = 1;
        self.multiTabIMV.alpha = 1;
        
        self.addressBT.alpha = 1;
        self.addressIMV.alpha = 1;
        
        self.backBT.alpha = 0;
        self.backIMV.alpha = 0;
        self.forwardIMV.alpha = 0;
        self.forwardBT.alpha = 0;
        
        self.unFullScreenBT.alpha = 1;
        self.unFullScreenIMV.alpha = 1;
        
        self.unFullScreenIMV.image = [UIImage imageNamed:@"ic_browser_fstoolbar_exit_nor.png"];
        self.backIMV.image = [UIImage imageNamed:@"ic_browser_toolbar_bw_nor.png"];
        self.forwardIMV.image = [UIImage imageNamed:@"ic_browser_toolbar_fw_nor.png"];
    }else{
        isInTabState = YES;
        
        self.multiTabBT.alpha = 0;
        self.multiTabIMV.alpha = 0;
        
        self.addressBT.alpha = 0;
        self.addressIMV.alpha = 0;
        
//        self.unFullScreenBT.alpha = 0;
//        self.unFullScreenIMV.alpha = 0;
        
        self.backBT.alpha = 1;
        self.backIMV.alpha = 1;
        self.forwardIMV.alpha = 0;
        self.forwardBT.alpha = 0;
        self.forwardBT.enabled = YES;
        self.forwardIMV.image = [UIImage imageNamed:@"ic_menu_desk_clock.png"];
        self.backBT.enabled = YES;
        self.backIMV.image = [UIImage imageNamed:@"ic_browser_tabwindow_back_nor.png"];
        self.unFullScreenIMV.image = [UIImage imageNamed:@"ic_browser_toolbar_newpage_nor.png"];
    }
}

- (IBAction)multiTabAction:(id)sender {
    if(delegate && [delegate respondsToSelector:@selector(onFullScreenPressMultiTabButton)]){
        [delegate onFullScreenPressMultiTabButton];
    }
}

- (IBAction)adressAction:(id)sender {
    if(delegate && [delegate respondsToSelector:@selector(onFullScreenPressAddressButton)]){
        [delegate onFullScreenPressAddressButton];
    }
}

-(void)initializeWithBrowser:(SGPageViewController *)_mainBrowser{
    self.alpha=0;
    self.browser = _mainBrowser;
    self.delegate = _mainBrowser;
    //[self.backBT addTarget:self.browser action:@selector(goBack) forControlEvents:UIControlEventTouchUpInside];
    //[self.forwardBT addTarget:self.browser action:@selector(goForward) forControlEvents:UIControlEventTouchUpInside];
    //[self.unFullScreenBT addTarget:self.browser action:@selector(goUnfullscreen) forControlEvents:UIControlEventTouchUpInside];
    
    self.backBT.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
    self.forwardBT.autoresizingMask = UIViewAutoresizingFlexibleRightMargin;
    self.unFullScreenBT.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin;
    
    self.multiTabBT.titleEdgeInsets = UIEdgeInsetsMake(2, 0, 0, 0);
    self.multiTabBT.titleLabel.font = [UIFont systemFontOfSize:11];
    [self.multiTabBT setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];


    self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
    isInTabState = NO;
    
}

-(void)fadeOut{
    [UIView animateWithDuration:fullScreenFadeDuration animations:^{
        self.alpha=0;
    }];
}
-(void)updateChrome{
    if(isInTabState == NO){
        self.backBT.enabled = self.browser.canGoBack;
        self.forwardBT.enabled = self.browser.canGoForward;
        [self updateBackState:self.browser.canGoBack AndForwardState:self.browser.canGoForward];
    }else{
        
    }
    
    
    NSString *text = [NSString stringWithFormat:@"%d", self.browser.count];
    [self.multiTabBT setTitle:text forState:UIControlStateNormal];

}

-(void)updateBackState:(BOOL)_backState AndForwardState:(BOOL)_forwardState{
    if(_backState==YES){
        self.backBT.alpha=1;
        self.backIMV.alpha=1;
    }else{
        self.backBT.alpha=0;
        self.backIMV.alpha=0;
    }
    
    if(_forwardState==YES){
        self.forwardBT.alpha=1;
        self.forwardIMV.alpha=1;
    }else{
        self.forwardBT.alpha=0;
        self.forwardIMV.alpha=0;
    }
    
    if([self.browser.selectedViewController isKindOfClass:[HomePageViewController class]]){
        HomePageViewController* homeVC = (HomePageViewController*)self.browser.selectedViewController;
        self.backBT.alpha=0;
        self.backBT.enabled = NO;
        self.backIMV.alpha=0;
        if(homeVC.lastURL != nil){
            self.forwardBT.alpha = 1;
            self.forwardIMV.alpha = 1;
            self.forwardBT.enabled = YES;
        }
        
    }else{
        self.backBT.alpha=1;
        self.backIMV.alpha=1;
        self.backBT.enabled = YES;
    }
}


-(void)needToShow:(BOOL)_need{
    if(_need){
        self.alpha=1;
        [fadeTimer invalidate];
        fadeTimer = [NSTimer scheduledTimerWithTimeInterval:maximumLastingTime target:self selector:@selector(fadeOut) userInfo:nil repeats:NO];
    }else{
        self.alpha = 0;
    }
}
@end
